<template>
  <a-layout
    :class="{
      layout:
        (store.userStore.layoutMode === 'topmenu' && store.userStore.contentWidth === 'Fixed') ||
        store.userStore.layoutMode !== 'topmenu'
    }"
    ref="layout"
  >
    <a-layout-sider
      class="asider"
      v-if="
        store.userStore.layoutMode === 'sidemenu' && (ScreenInfo.size.xl || ScreenInfo.size.xxl)
      "
      :theme="store.userStore.aside"
      width="168"
      collapsed-width="50"
      v-model:collapsed="state.collapsed"
      :style="{
        background: store.userStore.aside === 'dark' ? '#222222' : '#fff'
      }"
      :trigger="null"
      collapsible
    >
      <div class="logo" @click="gobaHome">
        <img v-if="state.collapsed == false" class="collapsed" src="@/assets/logo1.png" alt="" />
        <img v-else src="@/assets/logo2.png" alt="" />
      </div>
      <a-menu
        v-model:openKeys="state.openKeys"
        v-model:selectedKeys="state.selectedKeys"
        mode="inline"
        style="height: calc(100% - 48px)"
        :theme="store.userStore.aside"
        :style="{
          background: store.userStore.aside === 'dark' ? '#222222' : '#fff'
        }"
        class="menuList"
      >
        <template v-for="(item, index) in menuDatas" :key="index">
          <template v-if="item.meta.title && item.children && item.children.length > 0">
            <a-sub-menu class="item" :key="item.key">
              <template #title>
                <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                <span class="text">{{ item.meta.title }}</span>
              </template>
              <template v-for="itemTwo in item.children">
                <template v-if="!itemTwo.show">
                  <a-menu-item
                    v-if="!itemTwo.show"
                    :key="itemTwo.path"
                    @click="titleClick(itemTwo)"
                    class="subitem"
                  >
                    <!-- <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" /> -->
                    <span class="text">{{ itemTwo.meta.title }}</span>
                  </a-menu-item>
                </template>
              </template>
            </a-sub-menu>
          </template>
          <a-menu-item class="item" v-else :key="item.path" @click="titleClick(item)">
            <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
            <span class="text">{{ item.meta.title }}</span>
          </a-menu-item>
        </template>
      </a-menu>
    </a-layout-sider>
    <a-layout
      ref="containerRef"
      :class="{
        'layout-r': store.userStore.layoutMode === 'sidemenu'
      }"
    >
      <a-layout-header
        :class="{
          topmenu: store.userStore.layoutMode === 'topmenu',
          sticky: store.userStore.fixedHeader
        }"
        style="display: flex; flex-direction: column; width: 100%; height: auto; padding: 0"
      >
        <div style="display: flex; flex-direction: row; height: 48px">
          <img
            v-if="store.userStore.layoutMode === 'topmenu'"
            class="logo"
            src="@/assets/logo1.png"
            alt=""
          />
          <a-menu
            v-if="store.userStore.layoutMode === 'topmenu'"
            class="menuList-top"
            v-model:openKeys="state.openKeys"
            v-model:selectedKeys="state.selectedKeys"
            mode="horizontal"
            :theme="store.userStore.aside"
            :style="{ lineHeight: '48px' }"
          >
            <template v-for="(item, index) in menuDatas" :key="index">
              <template v-if="item.meta.title && item.children && item.children.length > 0">
                <a-sub-menu class="item" :key="item.key">
                  <template #title>
                    <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                    <span class="text">{{ item.meta.title }}</span>
                  </template>
                  <template v-for="itemTwo in item.children">
                    <template v-if="!itemTwo.show">
                      <a-menu-item
                        v-if="!itemTwo.show"
                        :key="itemTwo.path"
                        @click="titleClick(itemTwo)"
                        class="subitem"
                      >
                        <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                        <span class="text">{{ itemTwo.meta.title }}</span>
                      </a-menu-item>
                    </template>
                  </template>
                  <!-- <template v-for="(itemTwo, j) in menuDatas" :key="j + item.key" v-if="false">
                <a-menu-item>
                  <template #title>
                    <icon-font v-if="itemTwo.meta.icon" :type="itemTwo.meta.icon" />
                    {{ itemTwo.meta.title }}
                  </template>
                </a-menu-item>
              </template> -->
                </a-sub-menu>
              </template>
              <a-menu-item class="item" v-else :key="item.path" @click="titleClick(item)">
                <!-- <template #title>
                <span>{{ item.meta.title }}</span>
              </template> -->
                <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                <span class="text">{{ item.meta.title }}</span>
              </a-menu-item>
            </template>
          </a-menu>
          <RightContent
            :class="['headerTop', store.userStore.layoutMode === 'topmenu' ? 'on' : 'off']"
            :current-user="currentUser"
            v-model:collapsed="state.collapsed"
          />
        </div>
        <div
          v-if="store.userStore.hasTab"
          style="height: 40px"
          :style="{
            background:
              store.userStore.layoutMode === 'topmenu'
                ? store.userStore.aside === 'dark'
                  ? '#222222'
                  : '#fff'
                : token.colorBgBase,
            color:
              store.userStore.layoutMode === 'topmenu'
                ? store.userStore.aside === 'light'
                  ? '#001529'
                  : '#fff'
                : token.colorTextBase
          }"
        >
          <tabs />
        </div>

        <!--        <div-->
        <!--          :class="['layout-breadcrumb', state.collapsed == false ? 'on' : '']"-->
        <!--          v-if="router.currentRoute.value.path !== '/home'"-->
        <!--        >-->
        <!--          <a-breadcrumb>-->
        <!--            <a-breadcrumb-item v-for="(route, index) in breadcrumb" :key="route.path">-->
        <!--              <span v-if="router.currentRoute.value.path === route.path">-->
        <!--                <icon-font :type="route.icon" />-->
        <!--                {{ route.breadcrumbName }}-->
        <!--              </span>-->
        <!--              <router-link v-else :to="{ path: route.path }">-->
        <!--                <SmileOutlined />-->
        <!--                {{ route.breadcrumbName }}-->
        <!--              </router-link>-->
        <!--            </a-breadcrumb-item>-->
        <!--          </a-breadcrumb>-->
        <!--        </div>-->
      </a-layout-header>
      <a-layout-content
        class="content"
        :style="Object.assign({ color: token.colorTextBase }, layoutContentStyle)"
      >
        <RouterView
          :class="[
            'layout-content',
            state.collapsed == false ? 'on' : '',
            router.currentRoute.value.path != '/home' ? 'hidee' : 'showw'
          ]"
          v-slot="{ Component, route }"
        >
          <Transition name="page" mode="out-in">
            <keep-alive
              v-if="store.userStore.hasTab"
              :include="store.userApp.tabs.map((v) => v.name)"
            >
              <component :is="Component" :key="route.name" />
            </keep-alive>
            <component v-else :is="Component" :key="route.name" />
          </Transition>
        </RouterView>
      </a-layout-content>
    </a-layout>
  </a-layout>
  <a-drawer
    v-if="ScreenInfo.size.sm || ScreenInfo.size.md || ScreenInfo.size.lg"
    placement="left"
    width="168px"
    :bodyStyle="{
      padding: 0,
      overflow: 'hidden',
      background: store.userStore.aside === 'dark' ? '#222222' : '#fff'
    }"
    :closable="false"
    :open="state.collapsed"
    @close="state.collapsed = false"
  >
    <div class="asider drawer">
      <div class="logo" @click="gobaHome">
        <img class="collapsed" src="@/assets/logo1.png" alt="" />
      </div>
      <a-menu
        v-model:openKeys="state.openKeys"
        v-model:selectedKeys="state.selectedKeys"
        mode="inline"
        style="height: calc(100% - 48px)"
        :theme="store.userStore.aside"
        :style="{
          background: store.userStore.aside === 'dark' ? '#222222' : '#fff'
        }"
        class="menuList"
      >
        <template v-for="(item, index) in menuDatas" :key="index">
          <template v-if="item.meta.title && item.children && item.children.length > 0">
            <a-sub-menu class="item" :key="item.key">
              <template #title>
                <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                <span class="text">{{ item.meta.title }}</span>
              </template>
              <template v-for="itemTwo in item.children">
                <template v-if="!itemTwo.show">
                  <a-menu-item
                    v-if="!itemTwo.show"
                    :key="itemTwo.path"
                    @click="titleClick(itemTwo)"
                    class="subitem"
                  >
                    <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
                    <span class="text">{{ itemTwo.meta.title }}</span>
                  </a-menu-item>
                </template>
              </template>
            </a-sub-menu>
          </template>
          <a-menu-item class="item" v-else :key="item.path" @click="titleClick(item)">
            <icon-font class="icon" v-if="item.meta.icon" :type="item.meta.icon" />
            <span class="text">{{ item.meta.title }}</span>
          </a-menu-item>
        </template>
      </a-menu>
    </div>
  </a-drawer>
</template>
<script setup lang="ts">
import { useRouter, RouterView, RouterLink } from 'vue-router'
import { ref, reactive, onMounted, watch, computed, h } from 'vue'
import RightContent from './RightContent.vue'
import tabs from './components/Tabs.vue'
import { ScreenInfo } from '@/utils/device'
import IconFont from '@/components/IconFont/index.vue'

// const layoutStyle = computed(() => {
//   if (store.userStore.layoutMode === 'topmenu' && !store.userStore.fixedHeader) {
//     return {
//       'padding-top': '48px'
//     }
//   } else {
//     return {}
//   }
// })

import { theme } from 'ant-design-vue'
const { useToken } = theme
const { token } = useToken()

import store from '@/store'
const router = useRouter()
const state = reactive({
  menuWidth: '',
  collapsed: false,
  selectedKeys: [''],
  openKeys: [''],
  preOpenKeys: ['']
})
const menuDatas = computed(() => {
  const menuList = JSON.parse(JSON.stringify(store.userApp.menuList))
  for (let i = 0; i < menuList.length; i++) {
    menuList[i].title = menuList[i].name
    menuList[i].label = menuList[i].name
    if (menuList[i].children && menuList[i].children.length > 0) {
      for (let j = 0; j < menuList[i].children.length; j++) {
        menuList[i].children[j].title = menuList[i].children[j].name
        menuList[i].children[j].label = menuList[i].children[j].name
      }
    }
  }
  return menuList
})

watch(
  router.currentRoute,
  (val) => {
    console.log(val)
    if (router.currentRoute.value.path == '/home') {
      state.selectedKeys = []
    } else {
      const menuList = JSON.parse(JSON.stringify(store.userApp.menuList))
      for (let i = 0; i < menuList.length; i++) {
        if (menuList[i].children && menuList[i].children.length > 0) {
          for (let j = 0; j < menuList[i].children.length; j++) {
            if (menuList[i].children[j].path == router.currentRoute.value.path) {
              state.openKeys.push(menuList[i].key)
              state.selectedKeys = [menuList[i].children[j].path]
            }
          }
        } else {
          if (menuList[i].path == router.currentRoute.value.path) {
            state.selectedKeys = []
            state.selectedKeys.push(menuList[i].path)
          }
        }
      }

      store.userApp.tabsAdd(val)
    }
    store.userApp.tabsActiveKey = val.path
  },
  {
    immediate: true
  }
)
const titleClick = (item) => {
  const reg = new RegExp('^/(https|http|ftp)://')
  if (reg.test(item.path)) {
    window.open(item.path.substring(1))
  } else {
    router.push({
      path: item.path
    })
  }
}
const gobaHome = () => {
  router.push({
    path: '/home'
  })
}
const toggleCollapsed = () => {
  state.collapsed = !state.collapsed
  state.openKeys = state.collapsed ? [] : state.preOpenKeys
}
const currentUser = reactive({
  nickname: 'Admin',
  avatar: 'A'
})
const breadcrumb = computed(() => {
  return router.currentRoute.value.matched.concat().map((item) => {
    return {
      path: item.path,
      breadcrumbName: item.meta.title || '',
      icon: item.meta.icon
    }
  })
})

const layoutHeader = computed(() => {
  if (store.userStore.hasTab) {
    return 88
  } else {
    return 48
  }
})

const layoutContentStyle = computed(() => {
  console.log(layoutHeader)
  if (store.userStore.contentWidth === 'Fixed') {
    if (router.currentRoute.value.name === 'home') {
      return {
        height: `calc(100vh - ${layoutHeader.value}px)`,
        overflow: 'hidden auto'
      }
    } else {
      return {
        height: `calc(100vh - ${layoutHeader.value}px)`,
        overflow: 'hidden auto'
      }
    }
  } else {
    return {}
  }
})
</script>

<style lang="less" scoped>
.asider {
  .logo {
    img {
      width: 50px;
      height: 48px;
    }

    .collapsed {
      width: 168px;
      height: 48px;
    }
  }
}

.drawer {
  height: 100%;
}

.headerTop[class~='on'] {
  display: inline-flex;
  flex: 0 0 auto;
  flex-direction: row-reverse;
  justify-content: space-between;
}

.headerTop[class~='off'] {
  display: flex;
  flex: 1 1 auto;
  flex-direction: row-reverse;

  ::v-deep(.rightcontent) {
    width: 100%;
  }
}

.topmenu {
  z-index: 10;

  .logo {
    width: 168px;
    height: 48px;
  }
}

.topmenu {
  flex: 1 1 auto;
}

// .content {
//   overflow: hidden auto;
// }

.layout {
  position: absolute;
  inset: 0;
  height: 100vh;
  overflow: hidden;
}

.layout-r {
  position: relative;
  overflow: hidden auto;
}

.layout-content {
  position: relative;
  padding: 16px;
  overflow: hidden auto;
}

.sticky {
  position: sticky;
  top: 0;
  right: 0;
  z-index: 10;
}

.asider {
  .ant-menu {
    color: #86909c !important;
  }

  .menuList {
    ::v-deep(.item) {
      .ant-menu {
        color: #86909c !important;
      }

      padding-left: 12px !important;

      .ant-menu-submenu-title {
        padding-left: 12px !important;
      }
    }

    ::v-deep(.ant-menu-submenu.item) {
      padding-left: 0 !important;
    }

    ::v-deep(.subitem) {
      padding-left: 20px !important;
      // width: calc(100% - 3px);
    }
  }

  .menuList {
    overflow: -moz-scrollbars-none;
    overflow: hidden auto;
    scrollbar-width: none; /* firefox */
    -ms-overflow-style: none; /* IE 10+ */
  }

  .menuList::-webkit-scrollbar {
    display: none;
  }
}

.menuList-top {
  flex: 1 1 auto;
  width: 400px;
}
</style>
